package goods_manage.util;

import java.sql.*;

public class DBUtil {
    private static final String URL="jdbc:mysql://localhost:3306/merchandiesmanagesystem?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    private static final String DRIVER="com.mysql.jdbc.Driver";
    private static final String USER_NAME="root";
    private static final String PWD="508065";

    static {
        try {
            //驱动
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //获取链接
    public static Connection getConn(){
        try {
            return DriverManager.getConnection(URL,USER_NAME,PWD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //关闭链接
    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //关闭其他的
    public static void closePs(PreparedStatement ps){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void closeRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }



}
